package com.example.final_project.Cate.Activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.View;
import android.widget.ImageView;

import com.ejlchina.okhttps.OkHttps;
import com.example.final_project.Cate.adapter.CateCarAdapter;
import com.example.final_project.Cate.entry.CateCars;
import com.example.final_project.Internet.Internet;
import com.example.final_project.MainView;
import com.example.final_project.R;

import java.util.List;

public class CateCarActivity extends AppCompatActivity {

    private Context context;
    private ImageView back;
    private String name;
    private RecyclerView recyclerView;
    public static final int INIT=0;
    public List<CateCars> cateCars;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cate_car);
        Intent intent = this.getIntent();
        name = intent.getStringExtra("name");
        findById();
//        Internet.getInstance().getData();
        getData();
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context,RecyclerView.VERTICAL,false);
        recyclerView.setLayoutManager(linearLayoutManager);

        //返回点击事件
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent1 = new Intent(CateCarActivity.this, MainView.class);
                intent1.setAction("backToCate");
                startActivity(intent1);
            }
        });

    }

    private Handler handler = new Handler(Looper.getMainLooper()){
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            switch (msg.what){
                case INIT:
                    List<CateCars> list = (List<CateCars>)msg.obj;
                    CateCarAdapter cateCarAdapter = new CateCarAdapter(context,list);
                    recyclerView.setAdapter(cateCarAdapter);
                    break;
            }
        }
    };

    public void getData(){
        OkHttps.async("http://10.150.11.9:8086/chengcar/allCars")
                .bind(context)
                .addUrlPara("name",name)
                .setOnResList(CateCars.class,cars ->{
                    Message message = Message.obtain();
                    message.what=INIT;
                    message.obj=cars;
                    handler.sendMessage(message);
                })
                .get();
    }

    public void findById(){
        context = getBaseContext();
        back = this.findViewById(R.id.cate_back);
        recyclerView = this.findViewById(R.id.allCars);
    }

}